﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _Transmissionflattail2
    {
        public static void Transmissionflattail2(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层


            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);


            double scale = 1; //放大系数


            /*double d = 3.01;
            double a1 = 2.3;
            double l1 = 18;
            double l2 = 15;
            double s = 4.5;
            double l = 66.5;*/


            double d = Convert.ToDouble(data["dconms"]);
            double a1 = Convert.ToDouble(data["drvs"]);
            double l1 = Convert.ToDouble(data["l1"]);
            double l2 = Convert.ToDouble(data["lsc"]);
            double s = Convert.ToDouble(data["ldg"]);
            double l = Convert.ToDouble(data["oal"]);

            double tg = 0.05;
            double db = d * 2;


            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + l + 6, v1.Y);
            Vector2 v3 = new Vector2(v2.X - 3,v2.Y +a1/6);
            Vector2 v4 = new Vector2(v3.X, v3.Y + db / 2);
            Vector2 v5 = new Vector2(v4.X - l/1.333, v4.Y - (l * 3 / 4) * tg / 2);
            Vector2 v6 = new Vector2(v5.X - l / 11.428, v5.Y /2);
            Vector2 v7 = new Vector2(v3.X - l+0.5, v6.Y);
            Vector2 v8 = new Vector2(v3.X - l, v7.Y - 0.5);
            Vector2 v9 = new Vector2(v3.X, v3.Y * -1);
            Vector2 v10 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v11 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v12 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v13 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v14 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v15 = new Vector2(v3.X - l2 + a1 / 3, v3.Y);
            Vector2 v16 = new Vector2(v3.X - l2, v2.Y + a1 / 2);
            Vector2 v17 = new Vector2(v3.X - l1, v16.Y);
            Vector2 v18 = new Vector2(v17.X, v3.Y);
            Vector2 v19 = new Vector2(v3.X - l / 1.648, v3.Y);
            Vector2 v20 = new Vector2(v19.X, v19.Y * -1);
            Vector2 v21 = new Vector2(v18.X, v20.Y);
            Vector2 v22 = new Vector2(v17.X, v17.Y * -1);
            Vector2 v23 = new Vector2(v16.X, v16.Y * -1);
            Vector2 v24 = new Vector2(v15.X, v15.Y * -1);
            Vector2 v25 = new Vector2(v16.X, v2.Y + d / 2);
            Vector2 v26 = new Vector2(v3.X, v25.Y);
            Vector2 v27 = new Vector2(v25.X, v25.Y * -1);
            Vector2 v28 = new Vector2(v26.X, v27.Y);
            Vector2 v29 = new Vector2(v3.X - s, v10.Y - 3);
            Vector2 v30 = new Vector2(v29.X, v29.Y * -1);



            //
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v3));
            line2.Vertexes.Add(new LwPolylineVertex(v15));
            line2.Vertexes.Add(new LwPolylineVertex(v16));
            line2.Vertexes.Add(new LwPolylineVertex(v17));
            line2.Vertexes.Add(new LwPolylineVertex(v18));
            line2.Vertexes.Add(new LwPolylineVertex(v19));
            line2.Vertexes.Add(new LwPolylineVertex(v20));
            line2.Vertexes.Add(new LwPolylineVertex(v21));
            line2.Vertexes.Add(new LwPolylineVertex(v22));
            line2.Vertexes.Add(new LwPolylineVertex(v23));
            line2.Vertexes.Add(new LwPolylineVertex(v24));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes.Add(new LwPolylineVertex(v10));
            line2.Vertexes.Add(new LwPolylineVertex(v11));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v14));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v13));
            line2.Vertexes.Add(new LwPolylineVertex(v12));
            line2.Vertexes[0].Bulge = Math.Tan(25 * pi / 180 / 4);
            line2.Vertexes[4].Bulge = Math.Tan(66 * pi / 180 / 4);
            line2.Vertexes[8].Bulge = Math.Tan(320 * pi / 180 / 4);
            line2.Vertexes[12].Bulge = Math.Tan(66 * pi / 180 / 4);
            line2.Vertexes[16].Bulge = Math.Tan(25 * pi / 180 / 4);
            fun.PolyFill(dxf, line2, 0.1, contour);

            Line line3 = new Line(v16, v25);
            line3.Layer = refin;
            line3.Linetype = Linetype.Dashed;
            line3.LinetypeScale = 5;
            dxf.AddEntity(line3);

            Line line4 = new Line(v25, v26);
            line4.Layer = refin;
            line4.Linetype = Linetype.Dashed;
            line4.LinetypeScale = 5;
            dxf.AddEntity(line4);

            Line line5 = new Line(v23, v27);
            line5.Layer = refin;
            line5.Linetype = Linetype.Dashed;
            line5.LinetypeScale = 5;
            dxf.AddEntity(line5);

            Line line6 = new Line(v28, v27);
            line6.Layer = refin;
            line6.Linetype = Linetype.Dashed;
            line6.LinetypeScale = 5;
            dxf.AddEntity(line6);

            Line line7 = new Line(v30, v29);
            line7.Layer = centre;
            line7.Linetype = Linetype.DashDot;
            line7.LinetypeScale = 3;
            dxf.AddEntity(line7);


            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.5;
            dimensionStyle.ExtLineExtend = 0.6;//标注延长线长度/2
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            //文本样式
            dimensionStyle.TextHeight = 1;
            double offset = 8;


            LinearDimension dim1 = new LinearDimension(new Line(v4, v30), offset, 0, dimensionStyle);
            dim1.UserText = "LDG = " + s;
            dxf.AddEntity(dim1);

            LinearDimension dim2 = new LinearDimension(new Line(v26, v28), offset*2, -90, dimensionStyle);
            dim2.UserText = "dconms = " + d;
            dxf.AddEntity(dim2);

            LinearDimension dim3 = new LinearDimension(new Line(v16,v23), offset+(v3.X-v16.X ), -90, dimensionStyle);
            dim3.UserText = "DRVS = " + a1;
            dxf.AddEntity(dim3);

            LinearDimension dim4 = new LinearDimension(new Line(v27, v28), offset, 180, dimensionStyle);
            dim4.UserText = "LSC = " + l2;
            dxf.AddEntity(dim4);

            LinearDimension dim5 = new LinearDimension(new Line(v22, v10), offset*2 , 180, dimensionStyle);
            dim5.UserText = "l1 = " + l1;
            dxf.AddEntity(dim5);

            LinearDimension dim6 = new LinearDimension(new Line(v14, v10), offset * 3, 180, dimensionStyle);
            dim6.UserText = "OAL = " + l;
            dxf.AddEntity(dim6);

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }



            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}
